// 文件路径: src/main/java/cn/tedu/medicalmanagementsystem/manager/controller/UserController.java
package cn.tedu.medicalmanagementsystem.manager.controller;

import cn.tedu.medicalmanagementsystem.base.response.JsonResult;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.UserAddParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.UserDelParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.UserListQuery;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.UserUpdateParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.vo.UserVO;
import cn.tedu.medicalmanagementsystem.manager.service.ManagerUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Api(tags = "用户管理模块")
@RequestMapping("/v1/user")
public class ManagerUserController {

    @Autowired
    private ManagerUserService managerUserService;

    // 添加用户
    @PostMapping("/add")
    @ApiOperation("添加用户")
    public JsonResult add(@RequestBody UserAddParam userAddParam) {
        log.debug("开始处理【添加用户】的请求，参数：{}", userAddParam);

        // 调用服务层添加用户
        UserVO userVO = managerUserService.add(userAddParam);

        System.out.println("************************发送给前端的数据是："+JsonResult.ok(userVO).getMessage()+JsonResult.ok(userVO).getState());

        return JsonResult.ok(userVO);
    }

    // 更新用户
    @PostMapping("/update")
    @ApiOperation("修改用户信息")
    public JsonResult update(@RequestBody UserUpdateParam userUpdateParam) {
        log.debug("开始处理【更新用户】的请求，参数：{}", userUpdateParam);
        UserVO userVO = managerUserService.update(userUpdateParam);
        return JsonResult.ok(userVO);
    }

    // 根据ID删除用户
    @PostMapping("/delete/{id}")
    @ApiOperation("删除用户")
    public JsonResult delete(UserDelParam userDelParam) {
        log.debug("开始处理【删除用户】的请求，参数：{}", userDelParam);
        managerUserService.deleteById(userDelParam.getId());
        return JsonResult.ok();
    }


    //  查询用户列表
    @GetMapping("/users")
    @ApiOperation("查询用户列表")
    public JsonResult list() {
        log.debug("开始处理【查询用户列表】的请求");
        List<UserVO> list = managerUserService.allUsers();
        return JsonResult.ok(list);
    }
    //  根据ID查询用户
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询用户详情")
    public JsonResult getById(@PathVariable Long id) {
        log.debug("开始处理【根据ID查询用户】的请求，参数：{}", id);
        UserVO userVO = managerUserService.selectById(id);
        return JsonResult.ok(userVO);
    }

//    //根据用户名查询用户
//    @PostMapping("/name")
//    @ApiOperation("根据用户名查询用户")
//    public JsonResult getByUsername(@RequestBody UserListQuery user) {
//        log.debug("开始处理【根据用户名查询用户】的请求，参数：{}", user);
//        UserVO userVO = managerUserService.selectByUsername(user.getUsername());
//        return JsonResult.ok(userVO);
//    }

    @GetMapping("select")
    public JsonResult selectUser(UserListQuery userListQuery){
        log.debug("查询用户:userQuery={}",userListQuery);
        List<UserVO> list = managerUserService.select(userListQuery);
        return JsonResult.ok(list);
    }

}
